﻿namespace LiteBlog.Common.Entities
{
    using LiteBlog.Common.Components;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    using System.Linq;
    using System.Web;

    public class Post
    {        
        public Post()
        {
            Comments = new List<Comment>();
            Documents = new List<Document>();
            Categories = new List<Category>();
            Tags = new List<Tag>();
            Contents = string.Empty;
        }

        public string FileID { get; set; }
        public DateTime Time { get; set; }
        public DateTime UpdatedTime { get; set; }
        public string FormattedTime { get; set; }
        public string FormattedUpdatedTime { get; set; }
       
        [Required(ErrorMessage = "Please enter a title for the post")]
        [RegularExpression(@"[\w-\?!:,\. ]+", ErrorMessage = "No special characters allowed in the title")]
        public string Title { get; set; }
     
        public string Author { get; set; }
        
        public List<Comment> Comments { get; set; }
        public List<Document> Documents { get; set; }
        public List<Category> Categories { get; set; }
        public IList<Tag> Tags { get; set; }

        public PostInfo AsPostInfo()
        {
            var postInfo = new PostInfo();
            postInfo.FileID = FileID;
            postInfo.Title = Title;
            postInfo.Categories = Categories;
            postInfo.Time = Time;
            postInfo.UpdatedTime = UpdatedTime;
            postInfo.Author = Author;
            postInfo.Tags = Tags;
            return postInfo;
        }

        public string Contents { get; set; }
        public string Meta { get; set; }
        public string HighlightedContents { get; set; }

        private static readonly string DateTimeWebFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK";
        private static readonly List<string> ImageExtensions = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };
        public PostMeta GetMeta()
        {
            var postMeta = new PostMeta();
            postMeta.Title = Title;
            postMeta.Description = Meta ?? string.Empty;
            postMeta.Categories = new List<string>();
            if (Categories.Count > 0)
                postMeta.Categories = Categories.Select(c => c.Name).ToList();
            postMeta.Tags = new List<string>();
            if (Tags.Count() > 0)
                postMeta.Tags = Tags.Select(t => t.Name).ToList();
            postMeta.Images = new List<string>();
            foreach(var document in  Documents)
            {
                string extension = Path.GetExtension(document.VirtualPath).ToUpper();
                if (ImageExtensions.Contains(extension))
                {
                    string url = UrlHelper.AppUrl + document.VirtualPath.Substring(1);
                    postMeta.Images.Add(url);
                }
            }
            postMeta.Site = UrlHelper.Site;
            postMeta.Url = UrlHelper.GetUrl(UrlType.Post, FileID);
            postMeta.PublishedTime = ToWebFormat(Time); // Get the timezone from the config
            postMeta.UpdatedTime = ToWebFormat(UpdatedTime);
            return postMeta;
        }

        private string ToWebFormat(DateTime date)
        {
            var offset = new DateTimeOffset(date, DateTimeHelper.TimezoneOffset);
            return offset.ToString(DateTimeWebFormat);
        }
     }
}